#include "CGameApp.h"
#include "stdafx.h"
//--------------------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Structures
//--------------------------------------------------------------------------------------

struct ConstantBuffer
{
	XMMATRIX mWorld;
	XMMATRIX mView;
	XMMATRIX mProjection;
};

CGameApp::CGameApp(void)
{

}

void CGameApp::SetFramework( CFramework* pFramework )
{
	g_pFramework = pFramework;
}

BOOL CGameApp::Initialize()
{


	return TRUE;
}

void CGameApp::Release()
{

}

HRESULT CGameApp::OnCreateDevice( ID3D11Device* pD11Device, ID3D11DeviceContext* pD11DeviceContext, IDXGISwapChain* pSwapChain )
{
	g_pConstantBuffer.CreateConstantBuffer( pD11Device, pD11DeviceContext);
	g_pVertexShader.CompileVertexShader( pD11Device, pD11DeviceContext);
	g_pPixelShader.CompilePixelShader( pD11Device, pD11DeviceContext);
	g_pVertexBuffer.CreateVertexBuffer( pD11Device, pD11DeviceContext);
	g_pIndexBuffer.CreateIndexBuffer( pD11Device, pD11DeviceContext);

	    // Initialize the world matrix
	XMStoreFloat4x4(&g_World, XMMatrixIdentity());


    // Initialize the view matrix
	XMVECTOR Eye = XMVectorSet( 0.0f, 1.0f, -5.0f, 0.0f );
	XMVECTOR At = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
	XMVECTOR Up = XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f );
	XMStoreFloat4x4(&g_View, XMMatrixLookAtLH( Eye, At, Up ));

	RECT rc;
	GetClientRect(g_pFramework->GethWnd(), &rc );
	UINT width = rc.right - rc.left;
	UINT height = rc.bottom - rc.top;


    // Initialize the projection matrix
	XMStoreFloat4x4(&g_Projection, XMMatrixPerspectiveFovLH( XM_PIDIV2, width / (FLOAT)height, 0.01f, 100.0f ) );
	return S_OK;
}

void CGameApp::OnResetDevice( ID3D11Device* pD11Device )
{

}

void CGameApp::OnLostDevice()
{

}

void CGameApp::OnDestroyDevice()
{

}

void CGameApp::OnUpdateFrame( ID3D11DeviceContext* pD11DeviceContext, IDXGISwapChain* pSwapChain, float elapsedTime )
{

}

void CGameApp::OnRenderFrame( ID3D11DeviceContext* pD11DeviceContext, IDXGISwapChain* pSwapChain, ID3D11RenderTargetView* pRenderTargetView, ID3D11DepthStencilView* pDepthStencilView, float elapsedTime )
{
   // Update our time
    static float t = 0.0f;

        static DWORD dwTimeStart = 0;
        DWORD dwTimeCur = GetTickCount();
        if( dwTimeStart == 0 )
            dwTimeStart = dwTimeCur;
        t = ( dwTimeCur - dwTimeStart ) / 100.0f;

    //
    // Clear the back buffer
    //
    float ClearColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; //red, green, blue, alpha
    pD11DeviceContext->ClearRenderTargetView( pRenderTargetView, ClearColor );

    //
    // Clear the depth buffer to 1.0 (max depth)
    //
    pD11DeviceContext->ClearDepthStencilView( pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );


	XMStoreFloat4x4(&g_World, XMMatrixRotationY( t ));

    ConstantBuffer cb;
	cb.mWorld = XMMatrixTranspose( XMLoadFloat4x4(&g_World) );
	cb.mView = XMMatrixTranspose( XMLoadFloat4x4(&g_View) );
	cb.mProjection = XMMatrixTranspose( XMLoadFloat4x4(&g_Projection) );
	pD11DeviceContext->UpdateSubresource( g_pConstantBuffer.GetConstantBuffer(), 0, NULL, &cb, 0, 0 );

    //
    // Render the first cube
    //

	ID3D11Buffer* test = g_pConstantBuffer.GetConstantBuffer();
	
	pD11DeviceContext->VSSetShader( g_pVertexShader.GetVertexShader(), NULL, 0 );
	pD11DeviceContext->VSSetConstantBuffers( 0, 1, &test );
	pD11DeviceContext->PSSetShader( g_pPixelShader.GetPixelShader(), NULL, 0 );
	pD11DeviceContext->DrawIndexed( 36, 0, 0 );

    //
    // Present our back buffer to our front buffer
    //
	pSwapChain->Present( 0, 0 );
}

void CGameApp::ProcessInput( long xDelta, long yDelta, long zDelta, BOOL* pMouseButtons, BOOL* pPressedKeys, float elapsedTime )
{

}

//--------------------------------------------------------------------------------------
// Entry point to the program. Initializes everything and goes into a message processing 
// loop. Idle time is used to render the scene.
//--------------------------------------------------------------------------------------
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow )
{
	UNREFERENCED_PARAMETER( hPrevInstance );
	UNREFERENCED_PARAMETER( lpCmdLine );


	CGameApp* pApplication = new CGameApp();
	CFramework* pFramework = new CFramework( (CBaseApp*)pApplication );

	pApplication->SetFramework( pFramework );

	if( FAILED( pFramework->InitWindow( "zeenc", hInstance, 640, 480, TRUE  ) ) )
		return 0;

	XMMATRIX g_World1 = XMMatrixIdentity();

	pFramework->Run();

	//pFramework->CleanupDevice(); // SAFE_RELEASE macro

	return 0;
}